﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic.Web;
using LotteryVote.Entity;
using LotteryVote.Logic;
using LotteryVote.Utility;

namespace LotteryVote
{
    public partial class EditUserCommission : AgentBasePage
    {
        private int UserId;
        public int CommissionId { get; private set; }
        public UserCommission UserComm
        {
            get;
            private set;
        }

        private UserCommissionFacade _userCommFacade;
        public UserCommissionFacade UserCommFacade
        {
            get
            {
                if (_userCommFacade == null)
                    _userCommFacade = new UserCommissionFacade();
                return _userCommFacade;
            }
        }

        public IList<EnumDescriptionAttribute> CompanyTypeList;
        public IList<EnumDescriptionAttribute> GameTypeList;
        public IList<CommissionValue> CommissionValueList;
        public IList<CommissionGroupModel> UserCommissionGroup { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitParams();

        }

        void InitParams()
        {
            if (string.IsNullOrEmpty(Request["UserId"]))
                Response.Redirect("~/");
            UserId = int.Parse(Request["UserId"]);
            LotterySpeciesEnum specie = LotterySpeciesEnum.VietnamLottery;
            if (!string.IsNullOrEmpty(Request["Specie"]))
                specie = EnumHelper.GetEnum<LotterySpeciesEnum>(Request["Specie"]);
            Execute(() =>
            {
                UserFacade userFacade = new UserFacade();
                var user = userFacade.GetUser(UserId);
                CheckPermission(user);
                UserComm = UserCommFacade.GetUserCommission(user, specie);
                CommissionId = UserComm.CommissionId;
                GameTypeList = EnumHelper.GetDescription<GameTypeEnum>();
                CompanyTypeList = EnumHelper.GetDescription<CompanyType>();
                CommissionValueList = UserCommFacade.GetCommissionValue(UserComm.CommissionId);
                UserCommissionGroup = UserCommFacade.GetUserCommissionGroup(UserComm.UserId, (int)LotterySpeciesEnum.VietnamLottery);
            });
        }

        void CheckPermission(User user)
        {
            if (user.ParentId != CurrentUser.UserId)
                Response.Redirect("~/");
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            var items = JSONHelper.ToObject<List<CommissionValueItem>>(hd_submitValue.Value);
            UserCommFacade.UpdateUserCommission(items);
            InitParams();
        }

        public CommissionValue GetCommVal(int comTypeId, int gameTypeId)
        {
            return CommissionValueList.Find(it => it.CompanyType.Id == comTypeId && it.GameType.GameId == gameTypeId) ?? new CommissionValue();
        }
        public ConcreteCommission GetComm(CommissionGroupModel model, int comTypeId, int gameTypeId)
        {
            return model.ConcreteCommissionList.Where(it =>
                it.CompanyType.Id == comTypeId && it.GameType.GameId == gameTypeId).FirstOrDefault() ?? new ConcreteCommission();
        }
    }
}